<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class FileStatForm extends CMiniyunStatForm
{
    protected  function chartMethods(){
        return array("chartPie","chartHistogram");
    }
    
    public function chartPie(){
        $count        = 0;
        $officeCount  = 0;
        $imageCount   = 0;
        $deletedCount = 0;

        $userFile = new UserFile();
        $count = $userFile->count();        $officeCount = $userFile->officeCount();        $imageCount = $userFile->imageCount();        $deletedCount = $userFile->deletedCount();
                        $charOptions = array(
	  'theme' => 'gray',
	  'title' => array('text' => '实时文件分布图,('.$count.'文件数)'),
      'tooltip' => array(
			   'formatter' => 'js:function(){ return \'<b>\'+ this.point.name +\'</b>: \'+ this.y +\' 位 ，占\'+ForDight(this.percentage,1) +\' %\';}'
        ),
      'series' => array(
        array(
         'type' => 'pie',
         'data' => array(
        array('文档总数',(int)$officeCount),
        array('图片总数',(int)$imageCount),
        array('回收站文件总数',(int)$deletedCount),
        array('其他总数',(int)($count-$officeCount-$imageCount-$deletedCount)),
        ),
        )
        )
        );
        return $charOptions;

    }

    
    public function chartHistogram(){

                                $offIds     = FileVersion::model()->officeIds();
        $officeData = $this->_getSampleHistogramData($this->_getArray($offIds));
                                $imageIds   = FileVersion::model()->imageIds();
        $imageData = $this->_getSampleHistogramData($this->_getArray($imageIds));
                                $deleteData = $this->_getSampleHistogramData("'is_deleted'=true");
                                $totalData  = $this->_getSampleHistogramData();
        $charOptions = array(
            'theme' => 'gray',
            'title' => array('text' => '文件统计图'),
            'xAxis' => array(
            'categories' => array_keys($totalData)
        ),
            'yAxis' => array(
    			'title' => array('text' => '文件数')
        ),
            'series' => array(
        array('name' => '当前办公文档', 'data' => array_values($officeData)),
        array('name' => '当前图片文档', 'data' => array_values($imageData)),
        array('name' => '当前回收站', 'data' => array_values($deleteData)),
        array('name' => '所有文件', 'data' => array_values($totalData)),
        )
        );
        return $charOptions;
    }

    
    private function _getArray($arr)
    {
        $ids='';
        foreach ($arr as $key=>$item)
        {
            $ids.=$item['id'].', ';
        }
        $ids=substr($ids, 0, count($ids) - 3);

        if (empty($ids))
        {
            $ids = -1;
        }

        return 'version_id in (' . $ids . ')';
    }
    
    private  function _getSampleHistogramData($conditions = ''){
        if (strlen($conditions) > 0)
        {
            $conditions .= ' and ';
        }

        $space= 9;        $s = strtotime($this->startDate);
        $e = strtotime($this->endDate);
        $diff = (int)($e-$s)/(24*3600);
        if($diff>$space){
            $spaceValue = (int)$diff/$space;        }else{
            $spaceValue = 1;        }
        $x = array();
        for($item=$s; $item<=$e; $item+=$spaceValue*24*3600){
            array_push($x,date("Y-m-d",$item));
        }
        $data = array();        foreach($x as $index=>$currentDate){
            $data[$currentDate]=(int)($this->model->count($conditions . " created_at<='".$currentDate." 23:59:59'"));
        }
        return $data;

    }
}